package com.example.photo_album.service;

import com.alibaba.fastjson.JSONObject;
import com.example.photo_album.dao.FileDao;
import com.example.photo_album.entity.ImageGrupEntity;
import com.example.photo_album.utils.MyFileUploadUtils;
import com.example.photo_album.utils.QiniuUtli;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class FileServiceImpl implements IFileService{

    @Autowired
    MyFileUploadUtils fileUploadUtils;

    @Autowired
    FileDao fileDao;

    @Transactional
    @Override
    public JSONObject saveImage(List<MultipartFile> fls, String content) throws IOException {
//        QiniuUtli qiniuUtli = new QiniuUtli();
        JSONObject jsonObject = new JSONObject();
        ImageGrupEntity imageGrupEntity = new ImageGrupEntity();
        imageGrupEntity.setImage_desc(content);
        int cid = fileDao.saveImageInfo(imageGrupEntity); //返回文字描述ID
        System.out.println(imageGrupEntity.getId());
        jsonObject.put("cid",imageGrupEntity.getId());
        for (MultipartFile multipartFile:fls
        ) {
            System.out.println(multipartFile.getOriginalFilename());
            //上传文件到七牛云 返回七牛云上传的json
//            String resultUrl = qiniuUtli.upload(multipartFile); //返回值为json
//            jsonObject.getString(resultUrl);
            //保存数据到数据库 返回存储后的链接
            //获取到名称 描述 七牛云的图片链接

            //先插入文字描述，获取到批次，再把批次放入图片表中当外键 这里需要使用mybatis事务管理
        }
        return jsonObject;
    }

    //返回操作完成的ID
    public Integer saveImageInfo(){
        //插入数据库操作，返回完成后的id


        return 0;
    }


}
